package com.hsbc.dev.teamo4.sms.sm.service.impl;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.sm.dao.IPermissionDao;
import com.hsbc.dev.teamo4.sms.sm.dao.IRoleMenuPermissionDao;
import com.hsbc.dev.teamo4.sms.sm.dao.IUserRoleDao;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.PermissionDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.RoleMenuPermissionDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.UserRoleDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.po.Permission;
import com.hsbc.dev.teamo4.sms.sm.service.*;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.service.impl
 * @Description:
 * @Date date: 2018/7/29
 */
public class PermissionDistributeImpl implements IPermissionDistributeService{
    private IPermissionDao iPermissionDao=new PermissionDaoImpl();
    private IUserRoleDao iUserRoleDao=new UserRoleDaoImpl();
    private IRoleMenuPermissionDao iRoleMenuPermissionDao=new RoleMenuPermissionDaoImpl();


    @Override
    public List<Permission> getCurrentUserPermissionList(String userId) {
        return getUserPermissionList(userId);
    }

    @Override
    public List<Permission> getRolePermissionList(String roleId) {
        List<String> permissionIdList=iRoleMenuPermissionDao.getPermissionIdListByRoleIdList(Lists.newArrayList(roleId));
        if(CollectionUtils.isEmpty(permissionIdList)){
            return Lists.newArrayList();
        }
        return iPermissionDao.getPermissionByIdList(permissionIdList);
    }


    @Override
    public List<Permission> getUserPermissionList(String userId) {
        if(isSuperAdmin()){
            return iPermissionDao.queryAllEntity();
        }
        List<String> userRoleIdList=iUserRoleDao.getRoleIdListByUserId(userId);
        if(CollectionUtils.isEmpty(userRoleIdList)){
            return Lists.newArrayList();
        }
        List<String> userPermissionList=iRoleMenuPermissionDao.getPermissionIdListByRoleIdList(userRoleIdList);
        if(CollectionUtils.isEmpty(userPermissionList)){
            return Lists.newArrayList();
        }
        return iPermissionDao.getPermissionByIdList(userPermissionList);
    }

    @Override
    public boolean isSuperAdmin() {
        return true;
    }
}
